package com.wutong.authority_server.config;


import com.wutong.authority_server.filter.SecurityCorsFilter;
import com.wutong.authority_server.filter.TokenAuthFilter;
import com.wutong.authority_server.filter.TokenLoginFilter;
import com.wutong.authority_server.security.CustomAccessDeineHandler;
import com.wutong.authority_server.security.DefaultPasswordEncoder;
import com.wutong.authority_server.security.TokenLogoutHandler;
import com.wutong.authority_server.security.UnauthEntryPoint;
import com.wutong.authority_server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.access.channel.ChannelProcessingFilter;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    private RedisTemplate redisTemplate;
    private DefaultPasswordEncoder defaultPasswordEncoder;
    private UserDetailsService userDetailsService;
    private UserService userService;


    @Autowired
    public SecurityConfig(UserDetailsService userDetailsService, DefaultPasswordEncoder defaultPasswordEncoder,
                                  RedisTemplate redisTemplate,UserService userService) {
        this.userDetailsService = userDetailsService;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.redisTemplate = redisTemplate;
        this.userService = userService;
    }

    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    //设置退出的地址和token，redis操作地址
    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http.
                cors()//开启跨域处理
                .and()
                .csrf().disable()//禁用攻击拦截
                .exceptionHandling()
                .authenticationEntryPoint(new UnauthEntryPoint())//匿名用户访问无权限资源
                .accessDeniedHandler(new CustomAccessDeineHandler())//认证过的用户访问无权限资源
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)//禁用session
                .and()
                .authorizeRequests()
                //.antMatchers(HttpMethod.OPTIONS).permitAll()//处理跨域问题 方案一
                .anyRequest().authenticated()//任何白名单外的请求都需要登录认证
                .and()
                .logout().logoutUrl("/admin/acl/index/logout")//退出路径
                .addLogoutHandler(new TokenLogoutHandler(redisTemplate))
                .and()
                .addFilter(new TokenLoginFilter(authenticationManager(), redisTemplate))
                .addFilter(new TokenAuthFilter(authenticationManager(), redisTemplate,userService)).httpBasic()
                    .and()
                .addFilterBefore(new SecurityCorsFilter(), ChannelProcessingFilter.class);//处理跨域问题 方案二
    }

    //调用userDetailsService和密码处理
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }
    //不进行认证的路径，可以直接访问
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**");
    }


}
